import 'package:flutter/material.dart';
import 'package:flutter_wan_android/provider/provider_widget_consumer.dart';
import 'package:flutter_wan_android/provider/view_state_widget.dart';
import 'package:flutter_wan_android/ui/page/article/article_list_page.dart';
import 'package:flutter_wan_android/view_model/project_model.dart';
import 'package:flutter_wan_android/widget/flutter/drop_down_tab_bar.dart';

///
/// 项目页面
///
class ProjectPage extends StatefulWidget {
  @override
  _ProjectPageState createState() => _ProjectPageState();
}

class _ProjectPageState extends State<ProjectPage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ProviderWidgetConsumer<ProjectTreeModel>(
      model: ProjectTreeModel(),
      onModelReady: (model) => model.initData(),
      builder: (context, model, child) {
        if (model.isBusy) {
          return ViewStateBusyWidget();
        }
        if (model.isError) {
          return ViewStateErrorWidget(error: model.viewStateError, onPressed: model.initData);
        }
        return DropDownTabBar(model.dataList
            .map((projectTreeEntity) =>
                DropDownTabBarItem(
                    tabBarName: projectTreeEntity.name,
                    tabBarWidget: ArticleListPage(projectTreeEntity.id)))
            .toList());
      },
    );
  }
}
